package apiWeb

import (
	dbModel "ZHONGYIHANGYAN/bean/dbModel"
	requset "ZHONGYIHANGYAN/bean/request"
	quota "ZHONGYIHANGYAN/business/quota"
	"ZHONGYIHANGYAN/web"
	"fmt"

	"github.com/gin-gonic/gin"
)

func (api *Api) QuotaRouters(g *gin.RouterGroup) {
	routerG := g.Group("/quota")
	{
		// 获取指标列表
		routerG.POST("/getQuotaList", web.NewWebRouter(api.getQuotaList).Func)
		// 添加、修改指标列表
		routerG.POST("/addOrUpdateQuota", web.NewWebRouter(api.addOrUpdateQuota).Func)
		// 删除指标列表
		routerG.POST("/deleteQuota", web.NewWebRouter(api.deleteQuota).Func)
	}
}

func (api *Api) InitQuotaRouter() error {
	api.quotaManage = quota.Manage()
	api.web.RegRouters(api.QuotaRouters)
	return nil
}

// 获取指标列表
func (api *Api) getQuotaList(c *gin.Context, replayData *interface{}) error {
	query := requset.GetQuotaListQuery{}
	if err := api.web.GetPayload(c, &query); err != nil {
		return err
	}
	infoList, err := api.quotaManage.GetQuotaList(&query)
	if err != nil {
		return nil
	}
	*replayData = infoList
	return nil
}

// 添加、修改指标列表
func (api *Api) addOrUpdateQuota(c *gin.Context, replayData *interface{}) error {
	info := dbModel.Quota{}
	if err := api.web.GetPayload(c, &info); err != nil {
		return err
	}
	if err := validateParam(info); err != nil {
		return err
	}

	err := api.quotaManage.AddOrUpdateQuota(&info)
	if err != nil {
		return err
	}
	*replayData = info
	return nil
}

// 删除指标列表
func (api *Api) deleteQuota(c *gin.Context, replayData *interface{}) error {
	query := requset.DeleteQuotaQuery{}
	if err := api.web.GetPayload(c, &query); err != nil {
		return err
	}
	if len(query.QuotaIds) == 0 {
		return fmt.Errorf("请传入需要删除的指标id")
	}
	if err := api.quotaManage.DeleteQuota(query.QuotaIds); err != nil {
		return err
	}
	return nil
}
